package com.lottery.ruok.lottrey.ui;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.View;

import com.lottery.ruok.lottrey.R;

import utils.Utils;

/**
 * Created by ASUS on 2017/9/6.
 */

public class Wave extends View {
    // 默认属性值
    private static final int DEFAULT_AMPLITUDE = 200;
    private static final int DEFAULT_PERIOD = 16;
    private static final float DEFAULT_SPEED = .1F;
    private static final float DEFAULT_FREQUENCY = 1F / 2160F;
    private static final int DEFAULT_WAVE_COLOR = Color.parseColor("#ffffff");
    private static final int DEFAULT_WAVE_BK_COLOR = Color.parseColor("#00ffffff");
    @SuppressWarnings("FieldCanBeLocal")
    @ColorInt
    private int mWaveColor;
    @ColorInt
    private int mWaveBKColor;
    // 振幅
    private int mAmplitude;
    // 波浪的频率,这个值越大,波浪越密集
    private float mFrequency;
    // 速度
    private float mSpeed;
    private float mShift;
    private final Paint mWavePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Path mWavePath = new Path();

    public Wave(Context context) {
        this(context, null);
    }
    public Wave(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public Wave(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
    private void init(Context context, AttributeSet set) {
        final TypedArray array = context.obtainStyledAttributes(set, R.styleable.Wave);
        mSpeed = array.getFloat(R.styleable.Wave_speed, DEFAULT_SPEED);
        mWaveColor = array.getColor(R.styleable.Wave_waveColor, DEFAULT_WAVE_COLOR);
        mWaveBKColor = array.getColor(R.styleable.Wave_waveBackgroundColor, DEFAULT_WAVE_BK_COLOR);
        mAmplitude = array.getInt(R.styleable.Wave_amplitude, DEFAULT_AMPLITUDE);
        mFrequency = array.getFloat(R.styleable.Wave_frequency, DEFAULT_FREQUENCY);
        mFrequency = 1F / (2 * Utils.getWindowDisplay((Activity) context).widthPixels);
        array.recycle();

        mWavePaint.setStrokeWidth(2);
        mWavePaint.setColor(mWaveColor);
        // 开始波浪动画
//        postDelayed(new WaveAnimation(), DEFAULT_PERIOD);
    }

    public void startWave(){
        postDelayed(new WaveAnimation(), DEFAULT_PERIOD);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        final int width = getWidth();
        final int height = getHeight();
        mAmplitude = height/2;
        // 绘制背景
        canvas.drawColor(mWaveBKColor);
        mWavePath.moveTo(0, height);
        mWavePath.lineTo(0, height);
        for (int i = 1; i <= width; i++) {
            // 绘制正弦曲线 y = A Sin(ωt+ ρ) = A sin(2πft + ρ)
            if(type == 3) {
                if(is_t % 2 == 1) {
                    final float y = (float) (height / 2 + mAmplitude * Math.sin(2 * Math.PI * i * mFrequency + mShift + Math.PI * 3 / 2));
                    mWavePath.lineTo(i, y);
                } else {//反向
                    final float y = (float) (height / 2 + mAmplitude * Math.sin(2 * Math.PI * i * mFrequency - mShift + Math.PI * 3 / 2));
                    mWavePath.lineTo(i, y);
                }
            } else if(type == 1){
                final float y = (float) (height / 2 + mAmplitude * Math.sin(2 * Math.PI * i * mFrequency + Math.PI * 1 / 2));
                mWavePath.lineTo(i, y);
            } else if(type == 2){
                final float y = (float) (height / 2 + mAmplitude * Math.sin(2 * Math.PI * i * mFrequency + Math.PI * 3 / 2));
                mWavePath.lineTo(i, y);
            }
        }
        // 将曲线闭合
        mWavePath.lineTo(width, height);
        canvas.drawPath(mWavePath, mWavePaint);
    }

    public int is_t = 0;
    public int type = 2;//1,2,3
    final class WaveAnimation implements Runnable {
        @Override
        public void run() {
            mWavePath.reset();
            mShift += mSpeed;
            if((mShift <= Math.PI && is_t % 2 == 0) || (mShift <= 2 * Math.PI && is_t % 2 == 1)){
                Wave.this.postDelayed(this, DEFAULT_PERIOD);
                type = 3;
            } else {
                is_t++;
                if(is_t % 2 == 0){
                    type = 2;
                    mShift = 0;
                } else {
                    type = 1;
                    mShift = (float) Math.PI;
                }
            }
            invalidate();
        }
    }
}
